package com.clkj.sunhelp.util;

import android.content.Context;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.util.TypedValue;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.clkj.sunhelp.app.CustomApplication;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.net.FileNameMap;
import java.net.URLConnection;
import java.util.List;

/**
 * Created by Administrator on 2017/3/27.
 */

public class CommonUtil {


    public static SpannableString getAimTextColorSpannableStr(String originalStr, int firstTextColor, int secondTextColor, int firstTextIndex, int secondTextIndex)
    {
        SpannableString spannableString = new SpannableString(originalStr);
        ForegroundColorSpan tipColorSpan = new ForegroundColorSpan(firstTextColor);
        ForegroundColorSpan orderNoValueSpan = new ForegroundColorSpan(secondTextColor);
        spannableString.setSpan(tipColorSpan, firstTextIndex, secondTextIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(orderNoValueSpan, secondTextIndex, originalStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        return spannableString;
    }

    public static void putShareValue(String key, Object value) {
        SPUtils.put(CustomApplication.getInstance(), key, value);
    }

    public static Object getShareValue(String key, Object defaultObject) {
        return SPUtils.get(CustomApplication.getInstance(), key, defaultObject);
    }


    //初始化webview
    public static void initWebView(WebView webView)
    {
        WebSettings webSettings =   webView .getSettings();

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);

        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setDisplayZoomControls(false);
        webSettings.setJavaScriptEnabled(true); // 设置支持javascript脚本
        webSettings.setAllowFileAccess(true); // 允许访问文件
        webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
        webSettings.setSupportZoom(true); // 支持缩放


/**
 * 用WebView显示图片，可使用这个参数 设置网页布局类型： 1、LayoutAlgorithm.NARROW_COLUMNS ：
 * 适应内容大小 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
 *//*
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);*/
    }


    //格式化json数据变成类型 Object
    public static Object formatJsonToObject(JsonObject dataObject, String jsonKeyWord, TypeToken aimTypeToken) {
        return new Gson().fromJson(dataObject.get(jsonKeyWord), aimTypeToken.getType());
    }

    //格式化json数据变成类型 list
    public static List formatJsonToList(JsonObject dataObject, String jsonKeyWord, TypeToken aimTypeToken) {
        return new Gson().fromJson(dataObject.get(jsonKeyWord), aimTypeToken.getType());
    }


    public static boolean isStringEmpty(String validateStr, String tip) {
        if (TextUtils.isEmpty(validateStr)) {
            ToastUtil.showShort(CustomApplication.getInstance(), tip);
            return true;
        } else {
            return false;
        }
    }


    public static int getActionBarHeight(Context context) {

        int actionBarHeight = ((AppCompatActivity) context)
                .getSupportActionBar().getHeight();
        if (actionBarHeight != 0) {
            return actionBarHeight;
        }

        final TypedValue tv = new TypedValue();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (((AppCompatActivity) context).getTheme()
                    .resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(
                        tv.data, context.getResources().getDisplayMetrics());
            }
        }
        else {
            // 使用android.support.v7.appcompat包做actionbar兼容的情况
            if (((AppCompatActivity) context).getTheme()
                    .resolveAttribute(
                            android.support.v7.appcompat.R.attr.actionBarSize,
                            tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(
                        tv.data, context.getResources().getDisplayMetrics());
            }

        }


        return actionBarHeight;
    }


    public static int getStatusBarHeight(Context context)
    {
        /**
         * 获取状态栏高度——方法1
         * */
        int statusBarHeight = -1;
//获取status_bar_height资源的ID
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight = context.getResources().getDimensionPixelSize(resourceId);
        }
        Log.e("WangJ", "状态栏-方法1:" + statusBarHeight);

        return statusBarHeight;
    }

    //获取文件的类型
    public static String guessMimeType(String path)
    {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = fileNameMap.getContentTypeFor(path);

        if (contentTypeFor == null)
        {
            contentTypeFor = "application/octet-stream";
        }
        LogUtil.e("fileType:" + contentTypeFor);
        return contentTypeFor;
    }

}
